package tech.aistar.day09;

import tech.aistar.day09.homework.Purcase;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * 本类用来演示: TreeSet - 利用排序规则进行排序的
 *
 * @author: success
 * @date: 2022/7/29 10:52 上午
 */
public class TreeSetDemo {
    public static void main(String[] args) {
        Book b1 = new Book(1,"python",100.0d);
        Book b2 = new Book(2,"java",300.0d);
        Book b3 = new Book(3,"mysql",400.0d);
        Book b4 = new Book(4,"jdk",400.0d);

        //添加进去的对象必须要实现java.lang.Comparable接口
        //不可重复性和排序规则有关系 - price
        //认为price一样的,拒绝添加
        TreeSet<Book> set = new TreeSet<>();
        set.add(b1);
        set.add(b2);
        set.add(b3);
        set.add(b4);

        for (Book book : set) {
            System.out.println(book);
        }

        System.out.println("======");

        Purcase p1 = new Purcase("a","洗手粉",8.5);
        Purcase p2 = new Purcase("c","肥皂",4.5);
        Purcase p3 = new Purcase("b","牙膏",32.5);

        //Comparator<Purcase> c = (o1,o2)-> (int) (o2.getCost()-o1.getCost());
        Comparator<Purcase> c = ComparatorFactory.getInstance(ComparatorFactory.PRICE);

       // TreeSet<Purcase> sets = new TreeSet<>(c);
        TreeSet<Purcase> sets = new TreeSet<>(c);

        sets.add(p2);
        sets.add(p3);
        sets.add(p1);

        for (Purcase purcase : sets) {
            System.out.println(purcase);
        }
    }
}
